{
 "cells": [
  {
   "metadata": {
    "collapsed": true
   },
   "cell_type": "markdown",
   "source": [
    "Prompt Template是LangChain中的一个概念，接收用户输入，返回一个传递给LLM的信息，即提示词prompt。\n",
    "在应用开发中，固定的提示词限制了模型的灵活性和使用范围。因此Prompt Template是一个模板化字符串，你可以将变量插入到模板中，从而创建不同的提示。\n",
    "调用时：\n",
    "    以字典作为输入，其中每个键代表要填充的提示模板中的变量\n",
    "    输出一个PromptValue，这个PromptValue可以传递给LLM或者ChatModel，并且还可以转换为字符串或消息列表。"
   ],
   "id": "a85c7db774356058"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "#### 几种不同类型的提示词模板\n",
    "    PromptTemplate：LLM提示模板，用于生成字符串提示。它使用python的字符串来模板提示。\n",
    "    ChatPromptTemplate：聊天提示模板，用于组合各种角色的消息模板，传入聊天模型。\n",
    "    XxxMessagePromptTemplate：消息提示词模板，包括：SystemMessagePromptTemplate、HumanMessagePromptTemplate、AIMessagePromptTemplate、ChatMessagePromptTemperature等\n",
    "    FewShotPromptTemplate：样本提示词模板，通过示例来教模型如何回答\n",
    "    PiplinePrompt：管道提示词模板，用于把几个提示词组合在一起使用\n",
    "    自定义模板：允许基于其他模板类来制定自己的提示词模板"
   ],
   "id": "140e82d00856d1c1"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": " #### PromptTemplate使用，方式一(format)",
   "id": "3d811ae834f86ee4"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain_core.prompts import PromptTemplate\n",
    "\n",
    "# 参数中必须要指定：input_variables、template\n",
    "prompt_template = PromptTemplate(\n",
    "    template=\"你是一个{role},你的名字叫{name}\",\n",
    "    input_variables=[\"role\", \"name\"]\n",
    ")\n",
    "# 填充实例中的变量，暂且使用format()\n",
    "prompt = prompt_template.format(role=\"人工智能专家\", name=\"小智\")\n",
    "print(prompt)"
   ],
   "id": "4b00cc15b5f61881",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### PromptTemplate使用，方式二(from_template)：推荐",
   "id": "8a1d028b35c7ed52"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain_core.prompts import PromptTemplate\n",
    "\n",
    "prompt_template = PromptTemplate.from_template(template=\"你是一个{role},你的名字叫{name}\")\n",
    "prompt = prompt_template.format(role=\"人工智能专家\", name=\"小智\")\n",
    "print(prompt)"
   ],
   "id": "c743ad1841f39b36",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 如果提示词模板中不包含变量，则调用format()时，不需要传入参数",
   "id": "28ca2a3170e23e89"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain_core.prompts import PromptTemplate\n",
    "\n",
    "text = \"\"\"\n",
    "Tell me a joke\n",
    "\"\"\"\n",
    "prompt_template = PromptTemplate.from_template(text)\n",
    "prompt = prompt_template.format()\n",
    "print(prompt)"
   ],
   "id": "3e104c560bac006",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 部分提示词模板的使用",
   "id": "999cec5c2b9e864f"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain_core.prompts import PromptTemplate\n",
    "\n",
    "prompt_template = PromptTemplate.from_template(\n",
    "    template=\"你是一个{role},你的名字叫{name}\"\n",
    "    # partial_variables={\"role\": \"人工智能专家\"}\n",
    ").partial(role=\"人工智能专家\")\n",
    "\n",
    "prompt = prompt_template.format(name=\"小智\")\n",
    "print(type(prompt))\n",
    "print(prompt)"
   ],
   "id": "b82e739a14490edb",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 给变量赋值的两种方式：format()/invoke()",
   "id": "833aabca08660aa5"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain_core.prompts import PromptTemplate\n",
    "\n",
    "prompt_template = PromptTemplate.from_template(\n",
    "    template=\"你是一个{role},你的名字叫{name}\"\n",
    "    # partial_variables={\"role\": \"人工智能专家\"}\n",
    ").partial(role=\"人工智能专家\")\n",
    "\n",
    "prompt = prompt_template.invoke({\"name\":\"人工智能专家\"})\n",
    "print(type(prompt))\n",
    "print(prompt.text)"
   ],
   "id": "9b82c95cf28d8f95",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 结合大模型使用",
   "id": "5af5416cc074394e"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.prompts import PromptTemplate\n",
    "import os\n",
    "\n",
    "os.environ['OPENAI_BASE_URL'] = 'https://vip.apiyi.com/v1'\n",
    "os.environ['OPENAI_API_KEY'] = 'sk-xU64G4hXJ4L47ko3764958119dB245D2BdEcE528767dA1Da'\n",
    "\n",
    "chat_model = ChatOpenAI(model=\"gpt-4o-mini\", streaming=True)\n",
    "\n",
    "prompt_template = PromptTemplate.from_template(template=\"请评价{product}的优缺点，包括{aspect1}和{aspect2}\")\n",
    "\n",
    "prompt = prompt_template.format(product=\"智能手机\", aspect1=\"电池续航\", aspect2=\"拍照质量\")\n",
    "\n",
    "for chunk in chat_model.stream(prompt):\n",
    "    print(chunk.content, end=\"\", flush=True)"
   ],
   "id": "b7c50d38d7eff249",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
